<template>
  <div class="app-container">
    <gridview
      ref="gridView"
      :api-service="apiService"
      :grid-config="configData.gridConfig"
      :grid-handles="configData.gridHandles"
      :grid-data="testData"
      :filter-config="configData.filterConfig"
      :form-config="configData.formConfig"
      :form-data="configData.formModel"
      :grid-edit-width="300"
      form-title="用户"
      :is-async="true"
      @action="onAction"
    />
    <assign-dialog-form
      ref="dialogForm"
      :title="dialogTitle"
      :api-service="apiService"
      @submit="dialogSubmit"
    />
  </div>
</template>

<script>
import { USER_CONFIG } from './config'
import Gridview from '@/components/Gridview'
import AssignDialogForm from './components/AssignDialogForm'
export default {
  name: 'UserList',
  components: {
    Gridview,
    AssignDialogForm
  },
  data() {
    return {
      testData: [],
      configData: USER_CONFIG,
      apiService: this.$http.rbac.assignment,
      dialogFormVisible: false,
      dialogTitle: '编辑用户授权策略'
    }
  },
  created() {},
  mounted() {},
  methods: {
    getModel() {
      this.listLoading = true
      this.apiService.list(this.listQuery).then(response => {
        const { data } = response
        this.list = data.items
        this.total = data.meta.totalCount
        this.listLoading = false
      })
    },
    assign(row) {
      console.log('点击了下载按钮', row)
      this.$refs.dialogForm.getModel(row.id)
      this.$refs.dialogForm.showDialog()
    },
    // 模态框数据提交
    dialogSubmit(data) {
      this.$refs.gridView.refresh()
      this.$message.success(this.dialogTitle + '成功！')
    },
    onView(id) {
      this.$router.push({
        name: 'rbac/assignment/view/:id',
        params: {
          id: id
        }
      })
    },
    onUpdate(id) {
      this.$router.push({
        name: 'order/default/skus/:id',
        params: {
          id: id
        }
      })
    },
    onDelete(id) {
      this.$confirm('确认要删除当前管理员？')
        .then(_ => {
          this.delete(id)
        })
        .catch(_ => {})
    },
    delete(id) {
      console.log(id)
    },
    onAction(name, row) {
      if (name === 'view') {
        this.onView(row.id)
      } else if (name === 'update') {
        this.onUpdate(row.id)
      } else if (name === 'delete') {
        this.onDelete(row.id)
      } else if (name === 'assign') {
        this.assign(row)
      }
    }
  }
}
</script>
